package com.ta.controller;

import com.ta.pojo.Address;
import com.ta.pojo.BaseEntity;
import com.ta.pojo.District;
import com.ta.service.AddressService;
import com.ta.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;

    /*增加地址请求*/
    @RequestMapping("add_new_address")
    public JsonResult<Void> insertAddress(Address address, HttpSession session){
        addressService.insert(address, getUid(session), getUserName(session));
        return new JsonResult<>(200);
    }

    /*地址信息请求*/
    @RequestMapping({"/",""})
    public JsonResult<List<Address>> findByUid(HttpSession session){
        List<Address> addressList = addressService.findByUid(getUid(session));
        return new JsonResult<>(200, addressList);
    }

    /*设计默认地址请求*/
    @RequestMapping("modify_default/{aid}")
    public JsonResult<Void> modifyDefault(@PathVariable("aid") Integer aid, HttpSession session){
        addressService.ModifyDefaultAid(aid, getUid(session),getUserName(session));
        return new JsonResult<>(200);
    }

    /*删除地址请求*/
    @RequestMapping("delete/{aid}")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpSession session){
        addressService.delete(aid,getUid(session));
        return new JsonResult<>(200);
    }

    /*省市区信息请求*/
    @RequestMapping("/findby_city_parent")
    public JsonResult<List<District>> findByParent(String  parent){
        List<District> district = addressService.findByParent(parent);
        return new JsonResult<>(200,district);
    }

    /*修改地址请求*/
    @RequestMapping("/modify")
    public JsonResult<Void> modify(Address address, Integer aid, HttpSession session){
        addressService.modify(address, aid, getUserName(session),getUid(session));
        return new JsonResult<>(200);
    }


    @RequestMapping("/find_by_aid")
    public JsonResult<Address> findByAid(Integer aid){
        Address address = addressService.findByAid(aid);
        return new JsonResult<>(200,address);
    }

}
